package com.fer.hr.nmbp.lab2.gui;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.ibatis.session.SqlSession;

import com.hr.fer.nmbp.lab2.domain.Room;
import com.hr.fer.nmbp.lab2.domain.RoomExample;
import com.hr.fer.nmbp.lab2.persistence.RoomMapper;

public class ModifyRoomWindow extends JFrame{
	private static final long serialVersionUID = -3087804507460659966L;
	
	private JPanel contentPane;
	private JTextField tbRoomNum;
	private JTextField tbRoomSize;
	private JComboBox comboBox;
	private DefaultComboBoxModel model;
	private List<Room> rooms;
	private Room oldRoom;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ModifyRoomWindow frame = new ModifyRoomWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	private void fillRoomList(){
		SqlSession session = MainWindow.sessionFactory.openSession();
		RoomMapper mapper = session.getMapper(RoomMapper.class);
		
		rooms = mapper.selectByExample(null);
		
		for(int i=0;i<rooms.size();i++){
			model.insertElementAt(rooms.get(i), i);
		}
		session.commit();
		session.close();
	}
	
	private void closeClick(){
		this.dispose();
	}
	
	private void saveClick(){
		if(oldRoom==null || tbRoomNum.getText().equals("") || tbRoomSize.getText().equals("")){
			JOptionPane.showMessageDialog(this, "Niste unjeli sve potrebne podatke!", "Upozorenje", JOptionPane.ERROR_MESSAGE);
			closeClick();
		}else{
			SqlSession session = MainWindow.sessionFactory.openSession();
			RoomMapper mapper = session.getMapper(RoomMapper.class);
			
			Room newRoom = new Room(Integer.parseInt(tbRoomNum.getText()),Integer.parseInt(tbRoomSize.getText()));
			
			RoomExample re = new RoomExample();
			re.createCriteria().andRoomNumEqualTo(oldRoom.getRoomNum());
			
			mapper.updateByExample(newRoom, re);
			
			session.commit();
			session.close();
		}
	}
	/**
	 * Create the frame.
	 */
	public ModifyRoomWindow() {
		setTitle("Izmjena sobe");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 241, 220);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblOdaberiteSobu = new JLabel("Odaberite sobu:");
		lblOdaberiteSobu.setBounds(10, 11, 104, 14);
		contentPane.add(lblOdaberiteSobu);
		
		model = new DefaultComboBoxModel();
		comboBox = new JComboBox(model);
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				oldRoom = (Room)comboBox.getSelectedItem();
				tbRoomNum.setText(oldRoom.getRoomNum().toString());
				tbRoomSize.setText(oldRoom.getSize().toString());
			}
		});
		comboBox.setBounds(10, 36, 197, 20);
		contentPane.add(comboBox);
		fillRoomList();
		
		JLabel lblNoviBrojSobe = new JLabel("Novi broj sobe:");
		lblNoviBrojSobe.setBounds(10, 67, 104, 14);
		contentPane.add(lblNoviBrojSobe);
		
		tbRoomNum = new JTextField();
		tbRoomNum.setBounds(110, 64, 97, 20);
		contentPane.add(tbRoomNum);
		tbRoomNum.setColumns(10);
		
		JLabel lblNoviBrojKreveta = new JLabel("Novi broj kreveta:");
		lblNoviBrojKreveta.setBounds(10, 102, 104, 14);
		contentPane.add(lblNoviBrojKreveta);
		
		tbRoomSize = new JTextField();
		tbRoomSize.setBounds(110, 99, 97, 20);
		contentPane.add(tbRoomSize);
		tbRoomSize.setColumns(10);
		
		JButton btCancel = new JButton("Izlaz");
		btCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				closeClick();
			}
		});
		btCancel.setBounds(10, 142, 89, 23);
		contentPane.add(btCancel);
		
		JButton btSave = new JButton("Spremi");
		btSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				saveClick();
			}
		});
		btSave.setBounds(118, 142, 89, 23);
		contentPane.add(btSave);
	}
}
